/**
 * Created by admin on 2018/11/13.
 * 模拟网站用户登录
 *
 * */

const { Builder, By, Key, until } = require('selenium-webdriver');

(async function example() {
  const driver = await new Builder().forBrowser('chrome').build();
  try {
    const userName = '281471259@qq.com';
    const passwrod = 'TD123123td';
    const loginUrl = 'https://login.tongdun.cn/login.htm';

    await driver.get(loginUrl);
    await driver.findElement(By.id('loginId')).sendKeys(userName);
    await driver.findElement(By.id('password')).sendKeys(passwrod);
    await driver.findElement(By.id('login_btn')).click();
    await driver.get('https://credit.tongdun.cn/preloanentrance/creditapply/reporthistory/preLoanReportHistory.htm?operationType=queryReportList&curPage=1&pageSize=10&begin_time=2018-11-07%2000:00:00&end_time=2018-11-13%2011:03:55&invoke_mode=all&decision=all&search_cat=name&search_value=&rule_name=&group=&min_score=0&max_score=100&min_credit_score=&max_credit_score=');

    // driver.getPageSource().then((result) => {
    //   // 获取页面源码
    //   console.log(result);
    // });
    const el = await driver.findElement(By.className('report-table'));
    const tbody = await el.findElement(By.tagName('tbody'));
    const rows = await tbody.findElements(By.tagName('tr'));
    for (let i = 0; i < rows.length; i++) {
      const row = rows[j];
      row.findElements(By.tagName('td')).then((tds) => {
        tds.forEach((td) => {
          td.getText().then((result) => {
            console.log(result);
          }).catch((err) => {
            console.log(err);
          });
        });
      });
    }
  } finally {
    await driver.quit();
  }

  function buildCookieMap(cookies) {
    const map = {};
    cookies.forEach((cookie) => {
      map[cookie.name] = cookie;
    });
    return map;
  }
}());

